package com.fofia.iotweb.web.controllers.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.fofia.iotweb.Contants.SolonBootConfigDto;
import com.fofia.iotweb.cache.GatewayCacheHelper;
import com.fofia.iotweb.web.domain.service.FcDataService;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import com.fofia.iotweb.framework.core.RequestHelper;
import org.noear.solon.annotation.*;

@Controller
@Mapping("ruminate")
public class RuminateController {


    @Inject
    private  GatewayCacheHelper  gateCacheHelper;
    @Inject
    private RequestHelper requestHelper;
    @Inject
    FcDataService fcDataService;
    @Inject
    private SolonBootConfigDto solonBootConfigDto;
    /***
     *  历史数据按时间升序
     * @return
     */
    @Post
    @Mapping("getHistoryList")
    public ResultAdaptDto getHistoryList() {

        String tagId=requestHelper.getString("tagId","");
        int page=requestHelper.getInt("page",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        if(solonBootConfigDto.isApilog()) {
            StaticLog.info("来自Ip:{},getHistoryList:{} {} {} {},{}", requestHelper.getIp(), tagId, page, pageSize, stime, etime);
        }
        if(StrUtil.isEmpty(tagId)){
           return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            var list=fcDataService.getHistoryList(tagId,page,pageSize,stime,etime);

            var ret= JsonResult.success();
            ret.put("list",list);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }

    /***
     *  历史数据按时间降序
     * @return
     */
    @Post
    @Mapping("getHistoryDescList")
    public ResultAdaptDto getHistoryDescList() {

        String tagId=requestHelper.getString("tagId","");
        int page=requestHelper.getInt("page",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        if(solonBootConfigDto.isApilog()) {
            StaticLog.info("来自Ip:{},getHistoryDescList:{} {} {} {},{}", requestHelper.getIp(), tagId, page, pageSize, stime, etime);
        }
        if(StrUtil.isEmpty(tagId)){
            return JsonResult.failure("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.failure("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.failure("请输入结束时间");
        }

        try{
            var list=fcDataService.getHistoryDescList(tagId,page,pageSize,stime,etime);

            var ret= JsonResult.success();
            ret.put("list",list);
            return ret;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.failure();
    }


    /**
     * 分页数据，时间降序
     * @return
     */
    @Post
    @Mapping("getPageList")
    public DataTableDto getPageList() {

        String tagId=requestHelper.getString("tagId","");
        int page=requestHelper.getInt("page",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("stime","");
        String etime=requestHelper.getString("etime","");
        if(solonBootConfigDto.isApilog()) {
            StaticLog.info("来自Ip:{},getPageList:{} {} {} {},{}", requestHelper.getIp(), tagId, page, pageSize, stime, etime);
        }
        if(StrUtil.isEmpty(tagId)){
            return JsonResult.table("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.table("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.table("请输入结束时间");
        }

        try{
            var list=fcDataService.getPageList(tagId,page,pageSize,stime,etime);
            return list;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.table();
    }


    @Get
    @Mapping("getCache")
    public String getCache(){
        //String tmp = gateCacheHelper.getCache();
        String tmp = gateCacheHelper.getZset();
        return tmp;
    }
}
